package src.com.neusoft.nep.controller;

import src.com.neusoft.nep.common.AlertUtils;
import src.com.neusoft.nep.common.FileUtils;
import src.com.neusoft.nep.entity.DialogManager;
import src.com.neusoft.nep.entity.FeedbackData;
import src.com.neusoft.nep.entity.User;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class PublicSupervisorView2Controller {
    @FXML
    private TextField txt_EstPollutionLevel;
    @FXML
    private TextField txt_LocProvince;
    @FXML
    private TextField txt_LocCity;
    @FXML
    private TextField txt_FeedbackerName;
    @FXML
    private TextField txt_details;
    @FXML
    private TextField txt_phoneNumber;
    @FXML
    private Button btn_back;
    @FXML
    private Button btn_submit;
    @FXML
    private DatePicker feedbackDate;

    private Map<String, Object> feedbackData = new HashMap<>();

    public void setFeedbackData(Map<String, Object> data) {
        this.feedbackData.putAll(data);
    }

    @FXML
    public void handleSubmitButtonAction() {
        String estPollutionLevel = txt_EstPollutionLevel.getText();
        String locProvince = txt_LocProvince.getText();
        String locCity = txt_LocCity.getText();
        String feedbackerName = txt_FeedbackerName.getText();
        String details = txt_details.getText();
        String phoneNumber = txt_phoneNumber.getText();

        // 获取 DatePicker 的值并格式化为字符串
        LocalDate selectedDate = feedbackDate.getValue();
        if (selectedDate == null) {
            DialogManager.showDialog("错误", "请选择日期");
            return;
        }
        String feedbackDate = selectedDate.format(DateTimeFormatter.ISO_LOCAL_DATE); // 格式：YYYY-MM-DD

        FeedbackData feedbackData1 = new FeedbackData(feedbackerName,locProvince,locCity,estPollutionLevel,feedbackDate,phoneNumber,details);
        Map<String,FeedbackData> feedbackDataMap = FileUtils.readMapObject("FeedbackData.txt");
        feedbackDataMap.put(feedbackData1.getLocCity(),feedbackData1);
        FileUtils.writeMapObject("FeedbackData.txt",feedbackDataMap);
        DialogManager.showDialog("submit","提交成功");
    }


    @FXML
    private void back() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../view/PublicSupervisorView1.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_back.getScene();
        scene.setRoot(root);
        Stage stage = (Stage)scene.getWindow();
        stage.getScene();
        stage.show();
    }
}
